﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using RoyTools.D3Monitor.FormUI.Loot;
using RoyTools.D3Monitor.FormUI.Properties;
using System.Windows.Forms;
using RoyTools.D3Monitor.FormUI.Helper;

namespace RoyTools.D3Monitor.FormUI
{
    /// <summary>
    /// 日志监视类
    /// </summary>
    public class LogWatcher
    {
        /// <summary>
        /// 掉落计时器
        /// </summary>
        private TimerHelper<LootAnalyzerResultEntity> lootTimer { get; set; }

        /// <summary>
        /// 掉落分析结果
        /// </summary>
        private LootAnalyzerResultEntity currentLootAnalyzerResult { get; set; }

        /// <summary>
        /// 文件系统监视器
        /// </summary>
        private FileSystemWatcher fileSystemWatcher;

        /// <summary>
        /// 日志监视器设置
        /// </summary>
        private LogMonitorSetting logMonitorSetting { get; set; }

        /// <summary>
        /// 设置监视目录
        /// </summary>
        /// <param name="logDirectory"></param>
        public void SetMonitorDirectory(LogMonitorSetting logMonitorSetting)
        {
            this.logMonitorSetting = logMonitorSetting;
            LootMonitor(logMonitorSetting.LogDirectory);
        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            fileSystemWatcher = null;
        }

        /// <summary>
        /// 掉落监视器
        /// </summary>
        /// <param name="logDirectory"></param>
        private void LootMonitor(string logDirectory)
        {
            currentLootAnalyzerResult = new LootAnalyzerResultEntity();
            lootTimer = new TimerHelper<LootAnalyzerResultEntity>(Program.Config.MailSetting.SendInterval * 60000, new Action(BeforeSendMail), new Action(DoSendMail));
            lootTimer.Init();
            lootTimer.Start();
            fileSystemWatcher = new FileSystemWatcher(logDirectory, Resources.LootLogPattern);
            fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime;
            fileSystemWatcher.Changed += new FileSystemEventHandler(FileSystemWatcher_Event);
            fileSystemWatcher.Created += new FileSystemEventHandler(FileSystemWatcher_Event);
            fileSystemWatcher.EnableRaisingEvents = true;
        }

        /// <summary>
        /// 发送邮件之前
        /// </summary>
        private void BeforeSendMail()
        {
            lootTimer.BeforeData = currentLootAnalyzerResult;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        private void DoSendMail()
        {
            lootTimer.AfterData = currentLootAnalyzerResult;
            if (lootTimer.BeforeData.Equals(lootTimer.AfterData))
            {
                MailHelper.Send("TNT异常", "TNT名称：" + logMonitorSetting.Name + " 消息：" + Resources.LootLogNoResponse);
            }
        }

        /// <summary>
        /// 掉落日志事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileSystemWatcher_Event(object sender, FileSystemEventArgs e)
        {
            if (File.Exists(e.FullPath))
            {
                Program.Default.lsvLog.Invoke(new Action(delegate()
                {
                    Program.Default.lsvLog.BeginUpdate();
                    currentLootAnalyzerResult = LootAnalyzer.UpdateAnalyzerResult(new FileInfo(e.FullPath));
                    lootTimer.Stop();
                    lootTimer.BeforeData = currentLootAnalyzerResult;
                    lootTimer.Reset();
                    var listViewItem = DataHelper.EntityToListViewItem(logMonitorSetting.Name, currentLootAnalyzerResult);
                    var index = 0;
                    foreach (ListViewItem item in Program.Default.lsvLog.Items)
                    {
                        if (item.Text == listViewItem.Text)
                        {
                            index = item.Index;
                            break;
                        }
                    }
                    Program.Default.lsvLog.Items.RemoveAt(index);
                    Program.Default.lsvLog.Items.Insert(index, listViewItem);
                    Program.Default.lsvLog.EndUpdate();
                }));
            }
        }
    }
}
